package com.spike.service;

import com.spike.mapper.EmpMapper;
import com.spike.mapper.UserMapper;
import com.spike.model.Dept;
import com.spike.model.Emp;
import com.spike.model.User;
import com.spike.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class EmpService {
    public List<Dept> findAll(String name, String minSalary, String maxSalary,int start,int size) throws Exception {
        MyBatisUtils myBatisUtils = new MyBatisUtils();
        SqlSession sqlSession = myBatisUtils.getSqlSession();
        System.err.println(name);
        return sqlSession.getMapper(EmpMapper.class).findAll(name,minSalary,maxSalary,start,size);
    }
    public int addEmp(Emp emp) throws Exception {
        MyBatisUtils myBatisUtils = new MyBatisUtils();
        SqlSession sqlSession = myBatisUtils.getSqlSession();
        int i = sqlSession.getMapper(EmpMapper.class).addEmp(emp);
        sqlSession.commit();
        return i ;
    }

    public Emp findById(int i) throws Exception {
        MyBatisUtils myBatisUtils = new MyBatisUtils();
        SqlSession sqlSession = myBatisUtils.getSqlSession();
        Emp byId = sqlSession.getMapper(EmpMapper.class).findById(i);
        return byId;
    }
    public int updateById(Emp emp) throws Exception {
        MyBatisUtils myBatisUtils = new MyBatisUtils();
        SqlSession sqlSession = myBatisUtils.getSqlSession();
        int i = sqlSession.getMapper(EmpMapper.class).updateById(emp);
        sqlSession.commit();
        return i;
    }

    public int deleteEmp(int id) throws Exception {
        MyBatisUtils myBatisUtils = new MyBatisUtils();
        SqlSession sqlSession = myBatisUtils.getSqlSession();
        int i = sqlSession.getMapper(EmpMapper.class).deleteEmp(id);
        sqlSession.commit();
        return i;
    }

}
